package com.example.performance.r2dbc.entity.performance;

import com.example.performance.r2dbc.entity.RecordDb;
import lombok.NonNull;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static com.example.performance.r2dbc.entity.performance.RecordSetUtil.*;

/**
 * @author ShiSheng Gao
 * @since 2023/04/17
 */

public class VisibleRecordRowMapperByEnumAndIndex2 implements RowMapper<RecordDb> {

    private final List<String> visibleFields;

    public VisibleRecordRowMapperByEnumAndIndex2(List<String> visibleFields) {
        this.visibleFields = visibleFields;
    }

    @Override
    public @NonNull RecordDb mapRow(@NonNull ResultSet rs, int rowNum) throws SQLException {
        return from(rs);
    }

    private RecordDb from(ResultSet rs) throws SQLException {
        RecordDb.RecordDbBuilder builder = RecordDb.builder();

        builder.id(getLong(rs, 1));
        builder.nodeId(getLong(rs, 2));
        builder.statut(getStatus(rs, 3));

        for (int i = 0; i < visibleFields.size(); i++) {
            DDSFieldId ddsFieldId = DDSFieldId.valueOf(visibleFields.get(i));

            ddsFieldId.build(builder, rs, i + 4);
        }

        return builder.build();
    }

}
